package cn.wit.summit.chwan.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Alan on 2018/5/24.
 */

public class StringToUnicodeUtil {

    public static String stringToUnicode(String string) {
//        StringBuffer unicode = new StringBuffer();
//        for (int i = 0; i < string.length(); i++) {
//            char c = string.charAt(i);
//            if (isEmojiCharacter(c))
//                unicode.append("\\u" + Integer.toHexString(c));
//            else
//                unicode.append(c);
//        }
//        return unicode.toString();
        return string.replace("\\","\\\\");
    }

    /**
     * unicode 转字符串
     */
    public static String unicode2String(String unicode) {
        LogUtils.e("---m---unicode:"+unicode);
        StringBuffer string = new StringBuffer();
        int startIndex = 0;
        int cuIndex = unicode.indexOf("[EM-");//*第一个出现的索引位置
        while (cuIndex != -1) {
            LogUtils.e("---m---cuIndex:"+cuIndex);
            string.append(unicode.substring(startIndex, cuIndex));
            startIndex = cuIndex+9;
            String temp = unicode.substring(cuIndex+4, cuIndex+8);
            LogUtils.e("---m---temp:"+temp);
            int data = Integer.parseInt(temp, 16);
            string.append((char)data);
            cuIndex = unicode.indexOf("[EM-", startIndex);//*从这个索引往后开始第一个出现的位置
        }
        string.append(unicode.substring(startIndex));
        LogUtils.e("---m---string.toString():"+string.toString());
        return string.toString();
    }

    public static boolean isEmojiCharacter(char codePoint) {
        return !((codePoint == 0x0) ||
                (codePoint == 0x9) ||
                (codePoint == 0xA) ||
                (codePoint == 0xD) ||
                ((codePoint >= 0x20) && (codePoint <= 0xD7FF)) ||
                ((codePoint >= 0xE000) && (codePoint <= 0xFFFD)) ||
                ((codePoint >= 0x10000) && (codePoint <= 0x10FFFF)));
    }
}
